package rulebase;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

import javax.swing.DropMode;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;


public class RuleTree extends JScrollPane{
	
	JTree tree;
	DefaultMutableTreeNode root = new DefaultMutableTreeNode("Rules");
	File folder = new File("Rules");
	
	public RuleTree(){
		getFiles(folder, root);
        addComponents();
        
        this.setViewportView(tree);
        this.setVisible(true);
	}
	
	public void getFiles(File folder, DefaultMutableTreeNode root){
	    for (int i = 0; i < folder.listFiles().length; i++) {
	      if (folder.listFiles()[i].isFile()) {
	    	  root.add(new DefaultMutableTreeNode(folder.listFiles()[i].getName()));
	      }
	      else if (folder.listFiles()[i].isDirectory()){
	    	  setDirectory(folder.listFiles()[i], root);
	    	  System.out.println(folder.listFiles()[i]);
	      }
	    }
	}
	
	public void setDirectory(File folder, DefaultMutableTreeNode parent){
		DefaultMutableTreeNode newRoot = new DefaultMutableTreeNode(folder.getName());
		getFiles(folder, newRoot);
		parent.add(newRoot);
	}
	
	public void addComponents(){
        tree = new JTree(root);
        //tree.setSelectedFile(folder);
        tree.setDragEnabled(true);
        tree.setDropMode(DropMode.INSERT);
        tree.setTransferHandler(new TreeTransferHandler());
        tree.getSelectionModel().setSelectionMode(
                TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
        
        tree.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent event) {
				if (event.getClickCount() == 2) {
					DefaultMutableTreeNode temp = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
					System.out.println(temp.getParent());
					System.out.println((String)temp.getUserObject());
					
				}
			}
		});
        
	}
	
	
	public String getFilePath(DefaultMutableTreeNode temp){
		String filePath = "";
		
		
		return filePath;
	}
	
	
	
	
}
